<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" v-auth="'admin:role:edit'" icon="el-icon-plus" @click="handleData('add')"></el-button>
				<el-button v-auth="'admin:role:power'" plain :disabled="selection == null"
						   @click="permission">权限设置
				</el-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.name" placeholder="角色名称" clearable></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" row-key="role_id" @current-change="selectionChange"
					 highlight-current-row stripe>
				<el-table-column label="角色编码" prop="role_id" width="100"></el-table-column>
				<el-table-column label="角色名称" prop="name" width="150"></el-table-column>
				<el-table-column label="排序" prop="sort_order" width="80"></el-table-column>
				<el-table-column label="状态" prop="status" width="80">
					<template #default="scope">
						<el-switch v-model="scope.row.status" @change="changeSwitch($event, scope.row)"
								   :loading="scope.row.$switch_status" active-value="1" inactive-value="0" :disabled="scope.row.allow_edit==1"></el-switch>
					</template>
				</el-table-column>
				<el-table-column label="创建时间" prop="created_at" width="180"></el-table-column>
				<el-table-column label="备注" prop="remark" min-width="150"></el-table-column>
				<el-table-column label="操作" fixed="right" align="right" width="170">
					<template #default="scope">
						<div>
							<el-button-group>
								<el-button text type="primary" size="small" @click="handleData('show',scope.row, scope.$index)">
									查看
								</el-button>
								<el-button text type="primary" size="small" v-auth="'admin:role:edit'"
										   @click="handleData('edit',scope.row, scope.$index)" :disabled="scope.row.allow_edit==1">编辑
								</el-button>
								<el-button text type="primary" v-auth="'admin:role:edit'" size="small"
										   @click.stop="deleteRole('delete',scope.row, scope.$index)" :disabled="scope.row.allow_edit==1">删除
								</el-button>
							</el-button-group>
						</div>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess"
				 @closed="dialog.save=false"></save-dialog>

	<permission-dialog v-if="dialog.permission" ref="permissionDialog" @success="permissionSaveSuccess"
					   @closed="dialog.permission=false"></permission-dialog>

</template>

<script>
	import saveDialog from './save.vue'
	import permissionDialog from './permission.vue'

	export default {
		name: 'role',
		components: {
			saveDialog,
			permissionDialog
		},
		data() {
			return {
				dialog: {
					save: false,
					permission: false
				},
				apiObj: this.$API['admin.role'].list,
				selection: null,
				search: {
					name: null
				}
			}
		},
		methods: {
			//编辑
			handleData(mode, row = []) {
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open(mode).setData(row)
				})
			},
			//权限设置
			permission() {
				this.dialog.permission = true
				this.$nextTick(() => {
					this.$refs.permissionDialog.open().setData(this.selection)
				})
			},
			//表格选择后回调事件
			selectionChange(selection = []) {
				if (selection.role_id == 1) {
					this.$refs.table.clearSelection()
					this.selection = this.$refs.table.getSelectionRows()
					this.$alert('超级管理员无需设置权限', "提示", {type: 'error'})
				} else {
					this.selection = selection;
				}
			},
			//删除
			deleteRole(mode, row) {
				console.log(mode, row);
				this.$confirm(`注：确认删除角色吗？`, '提示', {
					type: 'warning',
					confirmButtonText: '确定',
					confirmButtonClass: 'el-button--danger'
				}).then(() => {
					this.$API['admin.role'].deleteRole.post({role_id: row.role_id})
						.then((res) => {
							if (res.code == 0) {
								this.$refs.table.refresh()
								this.$message.success("操作成功")
							} else {
								this.$alert(res.message, "提示", {type: 'error'})
							}
						})
						.catch((msg) => {
							this.$alert(msg, "提示", {type: 'error'})
						})
						.finally(() => {

						});
				}).catch(() => {
				})

			},
			//表格内开关
			changeSwitch(val, row) {
				row.$switch_status = true;
				this.$API['admin.role'].changeStatus.post({status: val, role_id: row.role_id})
					.then((res) => {
						if (res.code == 0) {
							this.$emit('success', this.form, this.mode)
							this.$message.success("操作成功")
							row.status = val;
						} else {
							this.$alert(res.message, "提示", {type: 'error'})
						}
					})
					.catch((msg) => {
						this.$alert(msg, "提示", {type: 'error'})
					})
					.finally(() => {
						row.$switch_status = false;
					});
			},
			//搜索
			upsearch() {
				this.$refs.table.upData(this.search)
			},
			handleSaveSuccess() {
				this.dialog.save = false
				this.$refs.table.refresh()
			},
			permissionSaveSuccess() {
				this.dialog.permission = false
				this.$refs.table.refresh()
			}
		}
	}
</script>

<style>
</style>
